package com.sihan.swvp.action.jc;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.entity.jc.JcBz;
import com.sihan.swvp.service.jc.JcBzService;
import com.sihan.swvp.service.sys.LogService;

@Controller
@RequestMapping("/jc/JcBzAction")
public class JcBzAction extends BaseAction{

	@Autowired
	private JcBzService jcBzService;
	@Autowired
	public LogService logService;
	
	@ResponseBody
	@RequestMapping("/getJcBz")
	public Object getJcBz(@Param("bzCode")String bzCode,@Param("bzName")String bzName,@Param("bzzyMajorId")String bzzyMajorId,@Param("page")Integer page,@Param("limit")Integer limit){
		try {
			Map<String, Object> params=new HashMap<String, Object>();
			params.put("bzCode", bzCode);
			params.put("bzName", bzName);
			params.put("bzzyMajorId", bzzyMajorId);
			return pageData(jcBzService.getJcBz(params,page,limit));
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg("服务器异常"); 
		}
	}
	
	@ResponseBody
	@RequestMapping("/addJcBz")
	public Object addJcBz(JcBz bean){
		try {
			bean.setUpuser(getLoginUser().getId());
			boolean isDsdwNameOk=jcBzService.getJcBzValidate(null, bean.getBzName(),bean.getId(),bean.getBzzyMajorId());
			boolean isCodeOk=jcBzService.getJcBzValidate(bean.getBzCode(), null,bean.getId(),bean.getBzzyMajorId());
			if(isDsdwNameOk){
				return failMsg("班组名称已存在");
			}else if(isCodeOk){
				return failMsg("班组编码已存在");
			}
			if(null == bean.getId()){
				jcBzService.addJcBz(bean);
				logService.addUserLog("添加班组", "班组", getLoginUser().getId(), "添加班组："+bean.getBzName(),getRemoteAddr(),1,0);
			}
			else{
				jcBzService.updateJcBz(bean);
				logService.addUserLog("修改班组", "班组", getLoginUser().getId(), "修改班组："+bean.getBzName(),getRemoteAddr(),1,0);
			}
			
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg("服务器异常");
		}
	}
	
	/*@ResponseBody
	@RequestMapping("/updateJcBz")
	public Object updateJcBz(JcBz bean){
		try {
			bean.setUpuser(getLoginUser().getId());
			boolean isDsdwNameOk=jcBzService.getJcBzValidate(null, bean.getBzName(),bean.getId(),bean.getBzzyMajorId());
			boolean isCodeOk=jcBzService.getJcBzValidate(bean.getBzCode(), null,bean.getId(),bean.getBzzyMajorId());
			if(isDsdwNameOk){
				return failMsg("班组名称已存在");
			}else if(isCodeOk){
				return failMsg("班组编码已存在");
			}
			jcBzService.updateJcBz(bean);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg(e.getMessage());
		}
	}*/
	
	@ResponseBody
	@RequestMapping("/deleteJcBz")
	public Object deleteJcBz(@Param("id")Integer id,String bzName){
		try {
			jcBzService.deleteJcBz(id);
			logService.addUserLog("删除班组", "班组", getLoginUser().getId(), "删除班组："+bzName,getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("删除班组", "班组", getLoginUser().getId(), "删除班组："+bzName,getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
}
